package com.quectel.aiot.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * 授权接口测试类
 * 演示如何使用修改后的获取对话实例接口
 */
public class AuthorizationTest {

    private static final String BASE_URL = "http://localhost:8080";
    private static final RestTemplate restTemplate = new RestTemplate();
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        System.out.println("=== 测试获取对话实例接口（支持voice配置） ===");
        
        // 测试用例1：使用实际的pk和dk（包含voice_id）
        testGetConversationInstanceWithVoice();
        
        // 测试用例2：使用实际的pk和dk（不包含voice_id）
        testGetConversationInstanceWithoutVoice();
        
        // 测试用例3：使用无效的参数
        testGetConversationInstanceWithInvalidParams();
        
        // 测试用例4：生成授权信息（向后兼容）
        testGenerateAuthorization();
    }

    /**
     * 测试获取对话实例（包含voice配置）
     * 使用实际的数据库数据：device ID 12649758851613696
     */
    private static void testGetConversationInstanceWithVoice() {
        System.out.println("\n--- 测试1：获取对话实例（包含voice配置） ---");
        
        try {
            // 构建请求体 - 使用实际的pk和dk
            // 根据数据库，device ID 12649758851613696 有 role_id=332723986683133952, voice_id=666
            String requestBody = "{\"pk\":\"p11vDa\",\"dk\":\"VDU1351652024184471\"}";
            
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            
            ResponseEntity<String> response = restTemplate.exchange(
                    BASE_URL + "/get-conversation-instance",
                    HttpMethod.POST,
                    entity,
                    String.class
            );
            
            System.out.println("响应状态码: " + response.getStatusCode());
            System.out.println("响应内容: " + response.getBody());
            
            // 解析响应，检查是否包含voice配置
            if (response.getStatusCode().is2xxSuccessful()) {
                String responseBody = response.getBody();
                if (responseBody != null && responseBody.contains("CUSTOM")) {
                    System.out.println("✅ 成功获取包含自定义voice配置的对话实例");
                } else if (responseBody != null && responseBody.contains("DEFAULT")) {
                    System.out.println("✅ 成功获取使用默认voice配置的对话实例");
                }
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试获取对话实例（不包含voice配置）
     * 使用实际的数据库数据：device ID 12649758901421056
     */
    private static void testGetConversationInstanceWithoutVoice() {
        System.out.println("\n--- 测试2：获取对话实例（不包含voice配置） ---");
        
        try {
            // 构建请求体 - 使用可能没有voice_id的设备
            // 根据数据库，device ID 12649758901421056 有 role_id 但没有 voice_id
            String requestBody = "{\"pk\":\"p11vhz\",\"dk\":\"0478756340DC\"}";
            
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            
            ResponseEntity<String> response = restTemplate.exchange(
                    BASE_URL + "/get-conversation-instance",
                    HttpMethod.POST,
                    entity,
                    String.class
            );
            
            System.out.println("响应状态码: " + response.getStatusCode());
            System.out.println("响应内容: " + response.getBody());
            
            if (response.getStatusCode().is2xxSuccessful()) {
                System.out.println("✅ 成功获取对话实例（使用默认配置）");
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试无效参数
     */
    private static void testGetConversationInstanceWithInvalidParams() {
        System.out.println("\n--- 测试3：使用无效参数 ---");
        
        try {
            // 构建请求体 - 缺少必要参数
            String requestBody = "{\"pk\":\"\"}";
            
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            
            ResponseEntity<String> response = restTemplate.exchange(
                    BASE_URL + "/get-conversation-instance",
                    HttpMethod.POST,
                    entity,
                    String.class
            );
            
            System.out.println("响应状态码: " + response.getStatusCode());
            System.out.println("响应内容: " + response.getBody());
            
            if (response.getStatusCode().is4xxClientError()) {
                System.out.println("✅ 正确处理了无效参数");
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试生成授权信息接口（保持向后兼容）
     */
    private static void testGenerateAuthorization() {
        System.out.println("\n--- 测试4：生成授权信息（向后兼容） ---");
        
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            
            HttpEntity<String> entity = new HttpEntity<>(headers);
            
            ResponseEntity<String> response = restTemplate.exchange(
                    BASE_URL + "/generate-authorization",
                    HttpMethod.POST,
                    entity,
                    String.class
            );
            
            System.out.println("响应状态码: " + response.getStatusCode());
            System.out.println("响应内容: " + response.getBody());
            
            if (response.getStatusCode().is2xxSuccessful()) {
                System.out.println("✅ 成功生成授权信息");
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}